package com.houtai.service.campus;


import com.houtai.base.RetInfo;
import com.houtai.bean.TdCampus;
import com.houtai.bean.dto.campus.CampusLogin;
import com.houtai.common.util.CommonUtil;
import com.houtai.common.util.MD5;
import com.houtai.dao.TdCampusDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class CampusService {
    private static Logger logger = Logger.getLogger(CampusService.class);
    @Autowired
    private TdCampusDao tdCampusDao;


    @Autowired
    private RedisTemplate redisTemplate;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * @Purpose   登录
     * @param     campusLogin，request
     * @version   1.0
     * @author    李寻欢
     * @time      2018-07-09
     * @return    RetInfo
     */
    public RetInfo login(CampusLogin campusLogin, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":login:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            if (redisTemplate.opsForValue().get(campusLogin.getSession_code()) != null) {
                TdCampus tdCampus = tdCampusDao.findTdCampusByAccount(campusLogin.getCampus_account());
                if (tdCampus != null ) {
                    String code = (String)redisTemplate.opsForValue().get(campusLogin.getSession_code());
                    if (campusLogin.getImg_code().toUpperCase().equals(code.toUpperCase())) {
                        if (tdCampus.getCampus_psd().equals(MD5.code(campusLogin.getCampus_psd()))) {
                            retInfo.setMark("0");
                            retInfo.setTip("登录成功");
                            Map<String, Object> map = new HashMap<>();
                            String token = CommonUtil.getToken();
                            map.put("token", token);
                            redisTemplate.opsForValue().set(token, tdCampus, 1, TimeUnit.DAYS);
                            retInfo.setObj(map);

                        } else {
                            retInfo.setMark("1");
                            retInfo.setTip("密码不对");
                        }
                    } else {
                        retInfo.setMark("2");
                        retInfo.setTip("验证码不对");
                    }
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("账号不存在");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("验证码失效");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @Purpose   修改园区
     * @param     TdCampus
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public synchronized RetInfo updateTdCampus(TdCampus TdCampus, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdCampus:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdCampus acount = tdCampusDao.findTdCampusByAccount(TdCampus.getCampus_account());
            if (acount != null) {
                if (acount.getCampus_id() == TdCampus.getCampus_id()) {
                    TdCampus.setSet_up_time(format.parse(TdCampus.getSet_up_time_s()));
                    tdCampusDao.updateTdCampus(TdCampus);
                    retInfo.setMark("0");
                    retInfo.setTip("成功.");

                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("该登录账号已存在");
                }

            } else {
                TdCampus.setSet_up_time(format.parse(TdCampus.getSet_up_time_s()));
                tdCampusDao.updateTdCampus(TdCampus);
                retInfo.setMark("0");
                retInfo.setTip("成功.");

            }


        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    /**
     * @Purpose   重置园区密码
     * @param     tdCampus
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTdCampusPsd(TdCampus tdCampus, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdCampusPsd:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            tdCampusDao.updateTdCampusPsd(tdCampus.getCampus_id(), tdCampus.getCampus_psd());
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            redisTemplate.opsForValue().set(request.getParameter("token"), tdCampus, 1, TimeUnit.DAYS);
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdCampus findTdCampusById(Integer campus_id) {
        return tdCampusDao.findTdCampusDetail(campus_id);
    }

}
